﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using DBQLTV;

namespace DAO
{
    public class CTPhieuNhapSachDAO
    {
        public bool InsertCTPhieuNhapSach(PHIEUNHAPSACH PhieuNhap, SACH Sach, int SoLuong, int DonGia)
        {
            using (DBQUANLYTHUVIENEntities entity = new DBQUANLYTHUVIENEntities())
            {
                CTPHIEUNHAPSACH CTPhieuNhap_temp = entity.CTPHIEUNHAPSACHes.FirstOrDefault(o => o.MaPhieuNS == PhieuNhap.MaPhieuNS && o.MaSach == Sach.MaSach && o.DonGia == DonGia);
                if (CTPhieuNhap_temp != null)// tồn tại
                {
                    UpdateSoLuong(CTPhieuNhap_temp, SoLuong);
                    return false;
                }
                CTPHIEUNHAPSACH CTPhieuNhap = new CTPHIEUNHAPSACH()
                {
                    MaPhieuNS = PhieuNhap.MaPhieuNS,
                    MaSach = Sach.MaSach,
                    SoLuong = SoLuong,
                    DonGia = DonGia
                };
                entity.CTPHIEUNHAPSACHes.AddObject(CTPhieuNhap);
                entity.SaveChanges();
                return true;
            }
        }
        private void UpdateSoLuong(CTPHIEUNHAPSACH ctPhieuNhap, int SoLuongThem)
        {
            using (DBQUANLYTHUVIENEntities entity = new DBQUANLYTHUVIENEntities())
            {
                ctPhieuNhap.SoLuong += SoLuongThem;
                entity.SaveChanges();
            }
        }
        public List<CTPHIEUNHAPSACH> GetCTPhieuNhapSach(int MaSach)
        {
            using (DBQUANLYTHUVIENEntities entity = new DBQUANLYTHUVIENEntities())
            {
                var list = from temp in entity.CTPHIEUNHAPSACHes where temp.MaSach == MaSach select temp;
                return list.ToList();
            }
        }
    }
}
